﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public static class KhoHang
    {
        public static Entities.KhoHang[] SelectKhoHang(Entities.KhoHang khohang)
        {
            Entities.KhoHang[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhoHang.sp_SelectKhoHang, cn);
                cmd.Parameters.Add(Common.Constants.KhoHang.HanhDong, SqlDbType.NVarChar).Value = khohang.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhoHang.MaKho, SqlDbType.VarChar).Value = khohang.MaKho;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KhoHang row = new Entities.KhoHang();
                    row.MaKho = dr[Common.Constants.KhoHang.MaKho].ToString();
                    row.TenKho = (dr[Common.Constants.KhoHang.TenKho].ToString());
                    row.DiaChi = dr[Common.Constants.KhoHang.DiaChi].ToString();
                    row.DienThoai = dr[Common.Constants.KhoHang.DienThoai].ToString();
                    row.MaNhanVien = dr[Common.Constants.KhoHang.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.KhoHang.TenNhanVien].ToString();
                    row.GhiChu = dr[Common.Constants.KhoHang.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.KhoHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.KhoHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhoHang)arr[i];
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return list;
        }
        public static Entities.KhoHang[] SelectAllKhoHang()
        {
            Entities.KhoHang[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhoHang.Deleted, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KhoHang row = new Entities.KhoHang();
                    row.MaKho = dr[Common.Constants.KhoHang.MaKho].ToString();
                    row.TenKho = (dr[Common.Constants.KhoHang.TenKho].ToString());
                    row.DiaChi = dr[Common.Constants.KhoHang.DiaChi].ToString();
                    row.DienThoai = dr[Common.Constants.KhoHang.DienThoai].ToString();
                    row.MaNhanVien = dr[Common.Constants.KhoHang.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.KhoHang.TenNhanVien].ToString();
                    row.GhiChu = dr[Common.Constants.KhoHang.GhiChu].ToString();
                    row.Deleted = bool.Parse(dr[Common.Constants.KhoHang.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.KhoHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhoHang)arr[i];
                    }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
            return list;
        }
        public static int Insert(Entities.KhoHang KhoHang)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhoHang.sp_InsertKhoHang, cn);
               // @MaKho,@TenKho,@DiaChi ,@DienThoai ,@TenNhanVien ,@MaNhanVien ,@GhiChu ,@Deleted
                cmd.Parameters.Add(Common.Constants.KhoHang.HanhDong, SqlDbType.NVarChar).Value = KhoHang.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhoHang.KhoHangID, SqlDbType.Int).Value = KhoHang.KhoHangID;
                cmd.Parameters.Add(Common.Constants.KhoHang.MaKho, SqlDbType.VarChar, 50).Value = KhoHang.MaKho;
                cmd.Parameters.Add(Common.Constants.KhoHang.TenKho, SqlDbType.NVarChar, 200).Value = KhoHang.TenKho;
                cmd.Parameters.Add(Common.Constants.KhoHang.DiaChi, SqlDbType.NVarChar, 200).Value = KhoHang.DiaChi;
                cmd.Parameters.Add(Common.Constants.KhoHang.DienThoai, SqlDbType.NVarChar, 20).Value = KhoHang.DienThoai;
                cmd.Parameters.Add(Common.Constants.KhoHang.TenNhanVien, SqlDbType.NVarChar,200).Value = KhoHang.TenNhanVien;
                cmd.Parameters.Add(Common.Constants.KhoHang.MaNhanVien, SqlDbType.VarChar,50).Value = KhoHang.MaNhanVien;
                cmd.Parameters.Add(Common.Constants.KhoHang.GhiChu, SqlDbType.NVarChar, 200 ).Value = KhoHang.GhiChu;
                cmd.Parameters.Add(Common.Constants.KhoHang.Deleted, SqlDbType.Bit).Value = KhoHang.Deleted;
                rt = cmd.ExecuteNonQuery();
               // cmd.Connection.Dispose();
               // cn.Close();
                //DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }
        public static int Update(Entities.KhoHang KhoHang)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhoHang.sp_UpdateKhoHang, cn);
                // @MaKho,@TenKho,@DiaChi ,@DienThoai ,@TenNhanVien ,@MaNhanVien ,@GhiChu ,@Deleted
                cmd.Parameters.Add(Common.Constants.KhoHang.HanhDong, SqlDbType.NVarChar).Value = KhoHang.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhoHang.KhoHangID, SqlDbType.Int).Value = KhoHang.KhoHangID;
                cmd.Parameters.Add(Common.Constants.KhoHang.MaKho, SqlDbType.VarChar, 50).Value = KhoHang.MaKho;
                cmd.Parameters.Add(Common.Constants.KhoHang.TenKho, SqlDbType.NVarChar, 200).Value = KhoHang.TenKho;
                cmd.Parameters.Add(Common.Constants.KhoHang.DiaChi, SqlDbType.NVarChar, 200).Value = KhoHang.DiaChi;
                cmd.Parameters.Add(Common.Constants.KhoHang.DienThoai, SqlDbType.NVarChar, 20).Value = KhoHang.DienThoai;
                cmd.Parameters.Add(Common.Constants.KhoHang.TenNhanVien, SqlDbType.NVarChar, 200).Value = KhoHang.TenNhanVien;
                cmd.Parameters.Add(Common.Constants.KhoHang.MaNhanVien, SqlDbType.VarChar, 50).Value = KhoHang.MaNhanVien;
                cmd.Parameters.Add(Common.Constants.KhoHang.GhiChu, SqlDbType.NVarChar, 200).Value = KhoHang.GhiChu;
                cmd.Parameters.Add(Common.Constants.KhoHang.Deleted, SqlDbType.Bit).Value = KhoHang.Deleted;
                rt = cmd.ExecuteNonQuery();
              //  cmd.Connection.Dispose();
              //  cn.Close();
              //  DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }
        public static int Delete(Entities.KhoHang KhoHang)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhoHang.sp_DeleteKhoHang, cn);
                // @MaKho,@TenKho,@DiaChi ,@DienThoai ,@TenNhanVien ,@MaNhanVien ,@GhiChu ,@Deleted
                cmd.Parameters.Add(Common.Constants.KhoHang.HanhDong, SqlDbType.VarChar).Value = KhoHang.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhoHang.MaKho, SqlDbType.VarChar, 50).Value = KhoHang.MaKho;
                rt = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }
    }
}
